<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item
                      @click.native="hAdd('')"
                    >添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 主体 -->
        <el-tree
          :data="list"
          :props="defaultProps"
          default-expand-all
          @node-click="handleNodeClick"
        >
          <template slot-scope="{ node, data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ node.label }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="hAdd(data.id)"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="hEdit(data.id)"
                        >编辑部门</el-dropdown-item>
                        <el-dropdown-item
                          v-if="data.children.length === 0"
                          @click.native="hDel(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog
      v-if="showDialog"
      :title="isEdit ? '编辑部门' : '添加部门'"
      :close-on-click-modal="false"
      :visible.sync="showDialog"
    >
      <dept-dialog
        :origin-list="originList"
        :pid="curId"
        :is-edit="isEdit"
        @success="hSuccess"
        @close="hCancel"
      />
    </el-dialog>
  </div>
</template>

<script>
import { delDepartment, getDepartments } from '@/api/departments'
import { arrAyToTree } from '@/utils'
import deptDialog from './deptDialog.vue'
export default {
  components: { deptDialog },
  data() {
    return {
      isEdit: false, // 是否添加还是编辑
      curId: '', // 保存id
      showDialog: false, // 控制弹层的显示和隐藏
      list: [], // 保存部门信息
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      originList: [] // 收集数据传给子组件
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    hCancel() {
      // 隐藏弹层
      this.showDialog = false
    },
    hSuccess() {
      // 隐藏弹层
      this.showDialog = false
      // 重新更新数据
      this.loadDepartments()
    },
    async doDel(id) {
      await delDepartment(id)
      this.$message.success('删除成功')
      // 重新更新数据
      this.loadDepartments()
    },
    hDel(id) {
      this.$confirm('您确定要删除吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          //  确认
          this.doDel(id)
        })
        .catch(() => {
          // 取消
        })
    },
    // 用户点击编辑部门
    hEdit(id) {
      // 显示弹层
      this.showDialog = true
      // 更新当前的操作id
      this.curId = id
      // 设置状态为编辑
      this.isEdit = true
    },
    // 用户点击添加部门
    hAdd(id) {
      // 显示弹层
      this.showDialog = true
      // 更新当前的操作id
      this.curId = id
      // 设置状态为添加
      this.isEdit = false
    },
    // 加载数据
    async loadDepartments() {
      // 查询企业的部门列表
      const res = await getDepartments()
      // 去掉第一项
      res.data.depts.shift()
      this.originList = res.data.depts.map((item) => {
        return {
          name: item.name,
          id: item.id,
          code: item.code,
          pid: item.pid
        }
      })
      this.list = arrAyToTree(res.data.depts)
    },
    handleNodeClick(data) {
      console.log(data)
    }
  }
}
</script>
